/**
 * 
 */
package com.baidu.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;

/**
 * @author liujie
 * 
 */
@SuppressLint("SimpleDateFormat")
public class TimeUtils {
    /**
     * 将形如"2013-01-10"的日期转为"2013年01月10日"，超过今年的会显示年份
     * 
     * @param date
     *            型如“2013-01-10”
     * @return "01月01日"
     */
    public static String parseReleaseTime(String date) {
        String result = "";
        try {
            if (!Tools.isEmpty(date)) {
                String[] dates = date.split("-");
                if (dates != null && dates.length == 3) {
                    result = dates[1] + "月" + dates[2] + "日";
                }
                Calendar ca = Calendar.getInstance();
                ca.setTime(new Date());
                int year = ca.get(Calendar.YEAR);
                
                if (year < Tools.strToInt(dates[0])) {
                    result = dates[0] + "." + dates[1] + "." + dates[2];
                    result = result.substring(2);
                }
            }
        } catch (Exception e) {
        }
        
        return result;
    }
    
    /**
     * 
     * 电话列表页面的时间格式转换
     * 
     * @param date
     *            输入字符串，要求是yyyy-MM-dd HH:mm:ss的格式
     * @param format
     *            输出格式
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String parseTime(String date, String format) {
        String result = null;
        
        if (!Tools.isEmpty(date)) {
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat targetFormat = new SimpleDateFormat(format);
            try {
                Date d = formater.parse(date);
                result = targetFormat.format(d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    /**
     * 
     * 详情页面的时间格式转换
     * 
     * @param date
     *            输入字符串，要求是yyyy-MM-dd HH:mm:ss的格式
     * @param format
     *            输出格式 14-03-03,18:00
     * @return
     */
    public static String getHistoryStyleTime(String date){
        
        String result = null;
        
        if (!Tools.isEmpty(date)) {
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat targetFormat = new SimpleDateFormat("MM-dd HH:mm");
            try {
                Date d = formater.parse(date);
                result = targetFormat.format(d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return result;
        
    }
    
    /**
     * 
     * 消息页面的时间格式转换
     * 
     * @param date
     *            输入字符串，要求是yyyy-MM-dd HH:mm:ss的格式
     * @param format
     *            输出格式 14-03-03,18:00
     * @return
     */
    
    public static String getMsgTitle(String date){
String result = null;
        
        if (!Tools.isEmpty(date)) {
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat targetFormat = new SimpleDateFormat("yy-MM-dd,HH:mm");
            try {
                Date d = formater.parse(date);
                result = targetFormat.format(d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public static long parseSystemTime(String date) {
        long result = 0L;
        
        if (!Tools.isEmpty(date)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date d = format.parse(date);
                result = d.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        
        return result;
    }
    
    public static String getReleaseDateInList(String date) {
        String title = "";
        
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        int yearOfToday = ca.get(Calendar.YEAR);
        int weekOfYear = ca.get(Calendar.WEEK_OF_YEAR);
        int dayOfToday = ca.get(Calendar.DAY_OF_YEAR);
        
        Date dateOfItem = null;
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
        try {
            dateOfItem = format.parse(date);
        } catch (ParseException e) {
        }
        
        if (dateOfItem != null) {
            ca.setTime(dateOfItem);
            int yearOfItem = ca.get(Calendar.YEAR);
            int weekOfItemYear = ca.get(Calendar.WEEK_OF_YEAR);
            int dayOfItem = ca.get(Calendar.DAY_OF_YEAR);
            
            if (yearOfToday == yearOfItem) {
                if (weekOfYear == weekOfItemYear) {
                    title = "本" + getCNLabelOfWeek(ca.get(Calendar.DAY_OF_WEEK)) + "上映";
                } else if (dayOfItem - dayOfToday <= 7) {
                    String week = getCNLabelOfWeek(ca.get(Calendar.DAY_OF_WEEK));
                    if (week != null && week.equals("周日") && dayOfItem != dayOfToday) {
                        title = "本" + week + "上映";
                    } else {
                        title = "下" + week + "上映";
                    }
                } else {
                    title = yearOfItem + "." + (ca.get(Calendar.MONTH) + 1) + "."
                        + (ca.get(Calendar.DAY_OF_MONTH)) + "日上映";
                }
            } else {
                title = yearOfItem + "." + (ca.get(Calendar.MONTH) + 1) + "."
                    + (ca.get(Calendar.DAY_OF_MONTH)) + "上映";
            }
            
        }
        return title;
    }
    
    /**
     * 
     * 电话列表页面的分组组名的样式
     * 
     * @param date
     *            输入字符串，要求是yyyy-MM-dd HH:mm:ss的格式
     * @return
     */
    public static String getDateTile(String date) {
        
        String result = "";
        
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        int yearOfToday = ca.get(Calendar.YEAR);
        int dayOfToday = ca.get(Calendar.DAY_OF_YEAR);
        
        Date dateOfItem = null;
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String prefixResult = "";
        try {
            dateOfItem = format.parse(date);
            String[] tmp = date.split(" ");
            prefixResult = tmp[0];
            
        } catch (Exception e) {
        }
        
        if (dateOfItem != null) {
            ca.setTime(dateOfItem);
            int yearOfItem = ca.get(Calendar.YEAR);
            int dayOfItem = ca.get(Calendar.DAY_OF_YEAR);
            
            if (yearOfToday == yearOfItem) {
                if (dayOfItem == dayOfToday) {
                    result = "今天";
                } else if (dayOfItem == dayOfToday - 1) {
                    result = "昨天";
                } else {
                    result = prefixResult;
                }
            } else {
                result = prefixResult;
            }
        }
        
        return result;
    }
    
    public static String getFullDate(long timeMillis) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(timeMillis);
        return format.format(ca.getTime());
    }
    
    public static String getCNLabelOfWeek(int dayOfWeek) {
        String result = "";
        switch (dayOfWeek) {
            case Calendar.MONDAY: {
                result = "周一";
                break;
            }
            case Calendar.TUESDAY: {
                result = "周二";
                break;
            }
            case Calendar.WEDNESDAY: {
                result = "周三";
                break;
            }
            case Calendar.THURSDAY: {
                result = "周四";
                break;
            }
            case Calendar.FRIDAY: {
                result = "周五";
                break;
            }
            case Calendar.SATURDAY: {
                result = "周六";
                break;
            }
            case Calendar.SUNDAY: {
                result = "周日";
                break;
            }
        }
        return result;
    }
}
